/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.controller.bean;

import com.biz.MailService;
import com.entites.ContactMessage;
import com.entites.Employee;
import com.model.bean.ContactMessageFacadeLocal;
import java.io.Serializable;
import java.util.Date;
import javax.ejb.EJB;
import javax.enterprise.context.SessionScoped;
import javax.faces.context.FacesContext;
import javax.inject.Named;
import javax.mail.MessagingException;
import javax.servlet.http.HttpSession;

/**
 *
 * @author VANANH
 */
@Named(value = "contactUs")
@SessionScoped
public class ContactUs implements Serializable
{

    @EJB
    private ContactMessageFacadeLocal contactMessageFacade;

    /**
     * Creates a new instance of ContactUs
     */
    public ContactUs()
    {
    }
    private static final String recipient = "anhntv_b01499@fpt.aptech.ac.vn";
    private String sender = "";
    private String subject;
    private String message;
    private String statusMessage = "";
    private String username;
    FacesContext fCtx = FacesContext.getCurrentInstance();
    HttpSession session = (HttpSession) fCtx.getExternalContext().getSession(true);

    public HttpSession getSession()
    {
        return session;
    }

    public void setSession(HttpSession session)
    {
        this.session = session;
    }

    public String getUsername()
    {
        return username;
    }

    public void setUsername(String username)
    {
        this.username = username;
    }

    public String getSender()
    {
        return sender;
    }

    public void setSender(String sender)
    {
        this.sender = sender;
    }

    public String getSubject()
    {
        return subject;
    }

    public void setSubject(String subject)
    {
        this.subject = subject;
    }

    public String getMessage()
    {
        return message;
    }

    public void setMessage(String message)
    {
        this.message = message;
    }

    public String getStatusMessage()
    {
        return statusMessage;
    }

    public void setStatusMessage(String statusMessage)
    {
        this.statusMessage = statusMessage;
    }

    public String send()
    {
        try
        {
            username = (String) session.getAttribute("username");
            MailService.sendEmail(sender, recipient, subject, message);
            Date date = new Date();
            ContactMessage cm = new ContactMessage(2, message, Boolean.FALSE,date,new Employee(username));
            contactMessageFacade.create(cm);
            statusMessage = "Message sent !";

        } catch (MessagingException ex)
        {
            statusMessage = "Can't Connect To Server !";
        }
        return "Contact";
    }
}
